package com.bw.kylin1705f.web;

import com.alibaba.fastjson.JSONObject;
import com.czb.crm.model.dto.request.AppHomeRequest;
import com.czb.crm.model.dto.response.*;
import com.czb.crm.util.common.Home;
import com.czb.crm.util.common.HttpUtils;
import com.czb.crm.util.common.Log;
import com.czb.crmsdk.util.response.WrapMapper;
import com.czb.crmsdk.util.response.Wrapper;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(AppHomeController.REQUEST_MAPPING)
@Slf4j
public class AppHomeController {

    static final String REQUEST_MAPPING = "/services/crmAppHome";
    //内网
    static final String POST_REQUEST_MAPPING = "http://test-data-center.czb365.com";
    //外网
    static final String OUT_POST_REQUEST_MAPPING = "http://test-data-center.czb365.com";

    /**
     *
     * APP查询当前各状态油站数量
     * */
    @PostMapping("/queryGasStatus")
    @Log(description = "APP查询当前各状态油站数量",method = "queryGasStatus",user = "test")
    public  Wrapper<CrmGasStatusResponse> queryGasStatus(@RequestBody @Validated AppHomeRequest request) {
        log.info("APP查询当前各状态油站数量");
        Wrapper<CrmGasStatusResponse> wrapper = null;
        String url="/services/datacenter/crmapp/queryGasStatus";
        try {
            if(Home.IsRequestNull(request)==false)
            {
                return WrapMapper.illegalArgument();
            }
            String api_result= HttpUtils.postWithBody(POST_REQUEST_MAPPING+url,5000,5000,new StringRequestEntity(JSONObject.toJSONString(Home.GetHome(request))
                    , "application/json", "utf-8"));
            if(StringUtils.isNotEmpty(api_result))
            {
                CrmGasResponse obj=JSONObject.parseObject(api_result,CrmGasResponse.class);
                if(obj.getCode()==Wrapper.SUCCESS_CODE)
                {
                    wrapper = WrapMapper.ok();
                    wrapper.setResult(obj.getResult());
                }
                else{

                    wrapper = WrapMapper.error();
                    wrapper.setResult(obj.getResult());
                    log.error("queryGasStatus api status is fail",obj.getCode());
                }
            }
            else{
                log.error("queryGasStatus api is null",api_result);
            }
        } catch (Exception ex) {
            log.error("queryGasStatus is fail ", ex.getMessage());
        }
        return wrapper;
    }
    /**
     *
     * APP查询昨日绩效
     * */
    @PostMapping("/queryHisPerformanceByDay")
    @Log(description = "APP查询昨日绩效",method = "queryHisPerformanceByDay")
    public Wrapper<CrmPerfromaceResponse> queryHisPerformanceByDay(@RequestBody @Validated AppHomeRequest request) {
        log.info("APP查询昨日绩效");
        Wrapper<CrmPerfromaceResponse> wrapper = null;
        String url="/services/datacenter/crmapp/queryHisPerformanceByDay";
        try {
            if(Home.IsRequestNull(request)==false)
            {
                return WrapMapper.illegalArgument();
            }

            String api_result= HttpUtils.postWithBody(POST_REQUEST_MAPPING+url,5000,5000,new StringRequestEntity(JSONObject.toJSONString(Home.GetHome(request))
                    , "application/json", "utf-8"));
            if(StringUtils.isNotEmpty(api_result))
            {
                CrmPerfromResponse obj=JSONObject.parseObject(api_result,CrmPerfromResponse.class);

                if(obj.getCode()==Wrapper.SUCCESS_CODE)
                {
                    wrapper = WrapMapper.ok();
                    wrapper.setResult(obj.getResult());
                }
                else{
                    wrapper = WrapMapper.error();
                    wrapper.setResult(obj.getResult());
                    log.error("queryGasStatus api status is fail",obj.getCode());
                }
            }
            else{
                log.error("queryGasStatus api is null",api_result);
            }

        } catch (Exception ex) {
            log.error("queryHisPerformanceByDay is fail ", ex.getMessage());
        }
        return wrapper;
    }

    /**
     *
     * APP查询当天绩效
     * */
    @PostMapping("/queryCurPerformance")
    @Log(description = "APP查询当天绩效",method = "queryCurPerformancegit")
    public Wrapper<CrmPerfromaceResponse> queryCurPerformance(@RequestBody @Validated AppHomeRequest request) {
        log.info("APP查询当天绩效");
        CrmPerfromaceResponse dto = null;
        Wrapper<CrmPerfromaceResponse> wrapper = null;
        String url="/services/datacenter/crmapp/queryCurPerformance";
        try {
            if(Home.IsRequestNull(request)==false)
            {
                return WrapMapper.illegalArgument();
            }
            String api_result= HttpUtils.postWithBody(POST_REQUEST_MAPPING+url,5000,5000,new StringRequestEntity(JSONObject.toJSONString(Home.GetHome(request))
                    , "application/json", "utf-8"));
            if(StringUtils.isNotEmpty(api_result))
            {
                CrmPerfromNowResponse obj=JSONObject.parseObject(api_result,CrmPerfromNowResponse.class);
                if(obj.getCode()==Wrapper.SUCCESS_CODE)
                {
                    wrapper = WrapMapper.ok();
                    wrapper.setResult(obj.getResult());
                }
                else{
                    wrapper = WrapMapper.error();
                    wrapper.setResult(obj.getResult());
                    log.error("queryCurPerformance api status is fail",obj.getCode());
                }
            }
            else{
                log.error("queryCurPerformance api is null",api_result);
            }

        } catch (Exception ex) {
            log.error("queryCurPerformance is fail ", ex.getMessage());
        }
        return wrapper;
    }

}
